home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / editor / chktex.lha / chktex / source / Resource.c < prev    next >
C/C++ Source or Header  |  1996-01-25  |  6KB  |  339 lines

  1. /*
  2.  *  ChkTeX v1.2, resource file reader.
  3.  *  Copyright (C) 1995-96 Jens T. Berger Thielemann
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  Contact the author at:
  20.  *        Jens Berger
  21.  *        Spektrumvn. 4
  22.  *        N-0666 Oslo
  23.  *        Norway
  24.  *        E-mail: <jensthi@ifi.uio.no>
  25.  *
  26.  *
  27.  */
  28.  
  29. #include "ChkTeX.h"
  30.  
  31. /***************************** RESOURCE HANDLING **************************/
  32.  
  33. static LONG BCount = 0;
  34.  
  35. /*
  36.  * Parses the contents of the `.chktexrc' file.
  37.  */
  38.  
  39. BOOL ReadRC(const STRPTR Filename)
  40. {
  41.     STRPTR    String;
  42.     BOOL    Success = FALSE;
  43.     FILE    *fh;
  44.  
  45.     BCount = 0;
  46.     if(fh = fopen(Filename, "r"))
  47.     {
  48.         while((!BCount++) && (String = ReadWord(ReadBuffer, fh)))
  49.         {
  50.             BCount--;
  51.               if(!stricmp(String, "SILENT"))
  52.                 CurRead = &Silent;
  53.             elif(!stricmp(String, "LINKER"))
  54.                 CurRead = &Linker;
  55.             elif(!stricmp(String, "ABBREV"))
  56.                 CurRead = &Abbrev;
  57.             elif(!stricmp(String, "IJACCENT"))
  58.                 CurRead = &IJAccent;
  59.             elif(!stricmp(String, "ITALIC"))
  60.                 CurRead = &Italic;
  61.             elif(!stricmp(String, "USERWARN"))
  62.                 CurRead = &UserWarn;
  63.             elif(!stricmp(String, "CMDLINE"))
  64.                 CurRead = &CmdLine;
  65.             elif(!stricmp(String, "POSTLINK"))
  66.                 CurRead = &PostLink;
  67.             else
  68.             {
  69.                 PrintPrgErr(pmKeyWord, ReadBuffer, Filename);
  70.  
  71.                 BCount = ~0;
  72.                 break;
  73.             }
  74.  
  75.             while(String = ReadWord(ReadBuffer, fh))
  76.             {
  77.                 ifn(InsertWord(String, CurRead))
  78.                 {
  79.                     PrintPrgErr(pmWordListErr);
  80.                     BCount = ~0;
  81.                     break;
  82.                 }
  83.             }
  84.         }
  85.         BCount--;
  86.         if(!BCount)
  87.             Success = TRUE;
  88.  
  89.         fclose(fh);
  90.     }
  91.     else
  92.         PrintPrgErr(pmRsrcOpen, Filename);
  93.  
  94.     return(Success);
  95. }
  96.  
  97. /*
  98.  * Translates escape codes. Give it a pointer to the char after the
  99.  * escape char, and we'll return what it maps to.
  100.  */
  101.  
  102. #define MAP(a,b)    case a: Tmp = b; break;
  103.  
  104. UBYTE MapChars(STRPTR *String)
  105. {
  106.     UBYTE    Chr, Tmp = 0;
  107.     UWORD    Cnt;
  108.  
  109.     Chr = *((STRPTR) (*String)++);
  110.  
  111.     switch(tolower(Chr))
  112.     {
  113.         MAP(QUOTE, QUOTE);
  114.         MAP(ESCAPE, ESCAPE);
  115.         MAP(CMNT, CMNT);
  116.         MAP('n', '\n');
  117.         MAP('r', '\r');
  118.         MAP('b', '\b');
  119.         MAP('t', '\t');
  120.         MAP('f', '\f');
  121.         MAP('{', '{');
  122.         MAP('}', '}');
  123.         MAP(' ', ' ');
  124.     case 'x':
  125.         Tmp = 0;
  126.  
  127.         for(Cnt = 0; Cnt < 2; Cnt++)
  128.         {
  129.             if(isxdigit(Chr = *((*String)++)))
  130.             {
  131.                 Chr = toupper(Chr);
  132.                 Tmp = (Tmp<<4) + Chr;
  133.  
  134.                 if(isdigit(Chr))
  135.                     Tmp -= '0';
  136.                 else
  137.                     Tmp -= 'A' - 10;
  138.             }
  139.             else
  140.             {
  141.                 if(Chr)    {
  142.                     PrintPrgErr(pmNotPSDigit, Chr, "hex");
  143.                     Tmp = 0;
  144.                 }
  145.                 break;
  146.             }
  147.         }
  148.         break;
  149.     case '0': case '1': case '2': case '3': case '4':
  150.     case '5': case '6': case '7':
  151.  
  152.         Tmp = Chr - '0';
  153.  
  154.         for(Cnt = 0; Cnt < 2; Cnt++)
  155.         {
  156.             Chr = *((*String)++);
  157.             if(within('0',Chr,'7'))
  158.                 Tmp = (Tmp * 8) + Chr - '0';
  159.             else
  160.             {
  161.                 if(Chr)
  162.                 {
  163.                     PrintPrgErr(pmNotPSDigit, Chr, "octal");
  164.                     Tmp = 0;
  165.                 }
  166.                 break;
  167.             }
  168.         }
  169.         break;
  170.     case 'd':
  171.         for(Cnt = 0; Cnt < 3; Cnt++)
  172.         {
  173.             if(isdigit(Chr = *((*String)++)))
  174.                 Tmp = (Tmp * 10) + Chr - '0';
  175.             else
  176.             {
  177.                 if(Chr)
  178.                 {
  179.                     PrintPrgErr(pmNotPSDigit, Chr, "");
  180.                     Tmp = 0;
  181.                 }
  182.                 break;
  183.             }
  184.         }
  185.         break;
  186.     default:
  187.         PrintPrgErr(pmEscCode, ESCAPE, Chr);
  188.     }
  189.     return(Tmp);
  190. }
  191.  
  192.  
  193. /*
  194.  * Reads a word from the `.chktexrc' file. Matches brackets. Uses
  195.  * ReadLine().
  196.  */
  197.  
  198. STRPTR ReadWord(STRPTR Buffer, FILE *fh)
  199. {
  200. static
  201.     STRPTR    String = NULL;
  202. static
  203.     UBYTE    StatBuf[BUFLEN];
  204.  
  205.     UWORD    Chr;
  206.  
  207.     STRPTR    Retval = NULL, Ptr = NULL;
  208.     BOOL    OnceMore = TRUE, Cont = TRUE;
  209.  
  210.     if(Buffer)
  211.     {
  212.         do
  213.         {
  214.             if(!String || !*String)
  215.             {
  216.                 String = ReadLine(StatBuf, fh);
  217.                 String = strip(String, STRP_RGT);
  218.             }
  219.  
  220.             Ptr = Buffer;
  221.             if(String = strip(String, STRP_LFT))
  222.             {
  223.                 switch(*String)
  224.                 {
  225.                 case '{':
  226.                     String++;
  227.                     BCount++;
  228.                     break;
  229.                 case '}':
  230.                     String++;
  231.                     BCount--;
  232.                     break;
  233.                 case CMNT:
  234.                     String = NULL;
  235.                     break;
  236.                 case 0:
  237.                     break;
  238.  
  239.                 case QUOTE:    /* Quoted argument */
  240.                     Cont = TRUE;
  241.                     String++;
  242.  
  243.                     while(Cont)
  244.                     {
  245.                         switch(Chr = *String++)
  246.                         {
  247.                         case 0:
  248.                         case QUOTE:
  249.                             Cont = FALSE;
  250.                             break;
  251.                         case ESCAPE:
  252.                             ifn(Chr = MapChars(&String))
  253.                                 break;
  254.  
  255.                             /* FALLTHRU */
  256.                         default:
  257.                             *Ptr++ = Chr;
  258.                         }
  259.                     }
  260.                     *Ptr = 0;
  261.                     Retval = Buffer;
  262.                     OnceMore = FALSE;
  263.                     break;
  264.                 default:    /* Non-quoted argument */
  265.  
  266.                     while(Cont)
  267.                     {
  268.                         switch(Chr = *String++)
  269.                         {
  270.                         case CMNT:
  271.                         case 0:
  272.                             String = NULL;
  273.                             Cont = FALSE;
  274.                             break;
  275.                         case ESCAPE:
  276.                             ifn(Chr = MapChars(&String))
  277.                                 break;
  278.  
  279.                             *Ptr++ = Chr;
  280.                             break;
  281.                         default:
  282.                             if(!isspace(Chr))
  283.                                 *Ptr++ = Chr;
  284.                             else
  285.                                 Cont = FALSE;
  286.                         }
  287.                     }
  288.  
  289.                     *Ptr = 0;
  290.                     Retval = Buffer;
  291.                     OnceMore = FALSE;
  292.                     break;
  293.                 }
  294.  
  295.                 if(BCount <= 0L)
  296.                 {
  297.                     Retval = NULL;
  298.                     OnceMore = FALSE;
  299.                     if(BCount < 0L)
  300.                         PrintPrgErr(pmBraceCnt, ConfigFile);
  301.                 }
  302.             }
  303.             else
  304.                 OnceMore = FALSE;
  305.         } while(OnceMore);
  306.     }
  307.     return(Retval);
  308. }
  309.  
  310. /*
  311.  * Reads a line from the `.chktexrc' file. Filters empty and comment
  312.  * lines.
  313.  */
  314.  
  315. STRPTR ReadLine(STRPTR Buffer, FILE *fh)
  316. {
  317.     STRPTR    Retval = NULL,
  318.             String;
  319.     BOOL    OnceMore = TRUE;
  320.  
  321.  
  322.     while(OnceMore && fgets(Buffer, BUFLEN-1, fh))
  323.     {
  324.         if(String = strip(Buffer, STRP_BTH))
  325.         {
  326.             switch(*String)
  327.             {
  328.             case 0:
  329.             case '#':
  330.                 break;
  331.             default:
  332.                 Retval = String;
  333.                 OnceMore = FALSE;
  334.             }
  335.         }
  336.     }
  337.     return(Retval);
  338. }
  339.